/**
 * Created by tang on 2018/12/10.
 */

import { routerRedux } from 'dva/router';
import { postList,postListSecond,postDel,postAdd,postUpdate,postGet  } from '../services/classify';
import { message } from 'antd';
import recode from '@/utils/recode';
import Cookies from 'universal-cookie';
const cookie = new Cookies();


export default {
  namespace: 'classify',

  state: {
    id: '',
    page: 1,
    pageSize: 10,
    total: 0,
    dataList: [],

    searchValue: '',
    searchKey: 'name',
    pid:0,
    nickName: '', //用户昵称
    name: '', //题目
    state: 0, //审核状态 1-审核中 2-通过 3-不通过
    type: 0, //类型 1-内部 2-外部
    status: 0, //项目状态 1-进行中 2-已完成 3-已取消
    classify:'',
    sort: 0, //排序
    point: '', //积分
  },

  effects: {
    //接口
    //一级分类列表
    *list({ payload }, { call, put, select }) {
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: true
      });
      //获取列表请求所需数据
      var state = yield select(state => state.classify);
      payload = {
        page: state.page,
        pageSize: state.pageSize,
        name: state.searchKey === 'name' ? state.searchValue : '',
        classify:state.classify
      };
      //网络请求
      const data = yield call(postList, payload);
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: false
      });
      //返回码
      // if (recode(data, put)){
      //   return;
      // }
      //数据存储
      yield put({
        type: 'save',
        payload: data.body
      });
    },
    //二级分类列表
    *listSecond({ payload }, { call, put, select }) {
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: true
      });
      //获取列表请求所需数据
      var state = yield select(state => state.classify);
      payload = {
        page: state.page,
        pageSize: state.pageSize,
        name: state.searchKey === 'name' ? state.searchValue : '',
        pid:state.pid
      };
      //网络请求
      const data = yield call(postListSecond, payload);
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: false
      });
      //返回码
      // if (recode(data, put)){
      //   return;
      // }
      //数据存储
      yield put({
        type: 'save',
        payload: data.body
      });
    },
    //删除
    *del({ payload, listType }, { call, put, select }) {
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: true
      });
      //网络请求
      const data = yield call(postDel, payload);
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: false
      });
      //返回码
      // if (recode(data, put)){
      //   return;
      // }
      //请求列表
      if(listType==1){
        yield put({
          type: 'list',
        });
      }else{
        yield put({
          type: 'listSecond',
        });
      }
    },
    //添加
    *add({ payload,listType }, { call, put, select }) {
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: true
      });
      //网络请求
      const data = yield call(postAdd, payload);
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: false
      });
      //返回码
      // if (recode(data, put)){
      //   return;
      // }
      message.success("添加成功!");
      //跳转列表
      if(listType==1){
        yield put(routerRedux.push('/setting/classify'));
      }else{
        yield put({
          type:'save',
          payload:{
            pid:payload.pid
          }
        });
        yield put(routerRedux.push('/setting/classifySecond?pid='+payload.pid));
      }

    },
    //编辑
    *update({ payload, listType }, { call, put, select }) {
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: true
      });
      //网络请求
      const data = yield call(postUpdate, payload);
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: false
      });
      // //返回码
      // if (recode(data, put)){
      //   return;
      // }
      //跳转列表
      if(listType==1){
        yield put(routerRedux.push('/setting/classify'));
      }else{
        yield put({
          type:'save',
          payload:{
            pid:payload.pid
          }
        });
        yield put(routerRedux.push('/setting/classifySecond?pid='+payload.pid));
      }
    },
    //get
    *get({ payload, callback }, { call, put, select }) {
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: true
      });
      //网络请求
      const data = yield call(postGet, payload);
      //loading状态修改
      yield put({
        type: 'changeLoading',
        payload: false
      });
      //返回码
      // if (recode(data, put)){
      //   return;
      // }
      //数据存储
      yield put({
        type: 'save',
        payload: {
          name:data.body.info.name,
          classify:data.body.info.classify
        }
      });
      if (callback) callback();
    },
  },

  reducers: {
    //数据处理
    //保存数据
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    //改变loading状态
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    //清除model数据
    clear(state){
      return {...state,
        id: '',
        page: 1,
        pageSize: 10,
        total: 0,
        dataList: [],

        nickName: '', //用户昵称
        title: '', //题目
        state: 0, //审核状态 1-审核中 2-通过 3-不通过
        type: 0, //类型 1-内部 2-外部
        status: 0, //项目状态 1-进行中 2-已完成 3-已取消
        name:'',
        sort: 0, //排序
        classify:'',
        point: '', //积分
      };
    }
  }
};
